package com.bkq.cb;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class EditLoanScreen extends Activity {
	private Button save,delete,back;
	private EditText name,dateFrom,dateTo,amount;
	String id;
	private SqliteServices service = null;
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.editloan);
	     save = (Button)findViewById(R.id.saveBnt_editloanScreen);
	     delete = (Button)findViewById(R.id.deleteBnt_editloanScreen);
	     back = (Button)findViewById(R.id.backIma_editloanScreen);
	     name = (EditText)findViewById(R.id.borrowerTxt_editloanScreen);
	     dateFrom = (EditText)findViewById(R.id.lendingdayTxt_editloanScreen);
	     dateTo = (EditText)findViewById(R.id.returndayTxt_editloanScreen);
	     amount = (EditText)findViewById(R.id.moneyTxt_editloanScreen);
	     service = new SqliteServices(this);
	     Bundle extras = getIntent().getExtras();
		    if (extras != null) 
		    {
		    	id = ( extras.getString("id").toString());
		    	name.setText( extras.getString("name").toString());
		    	amount.setText( extras.getString("amount").toString());
		    	dateFrom.setText( extras.getString("date").toString());
		    	dateTo.setText( extras.getString("date2").toString());
		    }
		    dateTo.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// TODO Auto-generated method stub
					 int y,m,d;
						Calendar c = Calendar.getInstance();        
				            y = c.get(Calendar.YEAR);        
				              m= c.get(Calendar.MONTH);        
				             d = c.get(Calendar.DAY_OF_MONTH); 
				             DatePickerDialog dp = new DatePickerDialog(EditLoanScreen.this,new DatePickerDialog.OnDateSetListener() {
				            	   @Override
				                   public void onDateSet(DatePicker view, int year,
				                           int monthOfYear, int dayOfMonth) {
				                      String erg = null;
				                        erg = String.valueOf(dayOfMonth);                      
				                        erg += "/" + String.valueOf(monthOfYear+1);                    
				                        erg += "/" + year;  
				                    
				                       dateTo.setText(erg);
				                   
				                   }
				            	   
				                 }, y, m, d);
				             
				              dp.setTitle("Choose Date");
				              dp.show();
				}
			});
	     dateFrom.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				 int y,m,d;
					Calendar c = Calendar.getInstance();        
			            y = c.get(Calendar.YEAR);        
			              m= c.get(Calendar.MONTH);        
			             d = c.get(Calendar.DAY_OF_MONTH); 
			             DatePickerDialog dp = new DatePickerDialog(EditLoanScreen.this,new DatePickerDialog.OnDateSetListener() {
			            	   @Override
			                   public void onDateSet(DatePicker view, int year,
			                           int monthOfYear, int dayOfMonth) {
			                      String erg = null;
			                        erg = String.valueOf(dayOfMonth);                      
			                        erg += "/" + String.valueOf(monthOfYear+1);                    
			                        erg += "/" + year;  
			                    
			                       dateFrom.setText(erg);
			                   
			                   }
			            	   
			                 }, y, m, d);
			             
			              dp.setTitle("Choose Date");
			              dp.show();
			}
		});
	     back.setOnClickListener(new View.OnClickListener() {
	   
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	     save.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					   service.openDatabase();
				if(service.updateLoan(Integer.parseInt(id), name.getText().toString(), dateFrom.getText().toString(), dateTo.getText().toString(), amount.getText().toString()))
				{
					Toast.makeText(getBaseContext(), "Edit successfully", Toast.LENGTH_SHORT).show();
					startActivity(new Intent(EditLoanScreen.this,CashBookScreen.class));
				}
				else 
				{
					Toast.makeText(getBaseContext(), "Edit failure", Toast.LENGTH_SHORT).show();
				}
				}
			});
		     delete.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					 service.openDatabase();
					if(service.deleteLoan(Integer.parseInt(id)))
					{
						Toast.makeText(getBaseContext(), "Delete successfully", Toast.LENGTH_SHORT).show();
						startActivity(new Intent(EditLoanScreen.this,CashBookScreen.class));
					}
					else 
					{
						Toast.makeText(getBaseContext(), "Delete failure", Toast.LENGTH_SHORT).show();
					}
				}
			});
	}
}
